package ops.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import ops.WindowManager;
import ops.model.Class;
import ops.model.EntityAlreadyExistsException;
import ops.model.Subscription;
import ops.model.manager.ClassManager;
import ops.model.manager.EntityController;
import ops.model.manager.SubjectManager;
import ops.model.manager.SubscriptionManager;
import ops.model.manager.TeacherManager;
import ops.view.SubscriptionPanel;

public class SubscriptionController {
	
	private SubscriptionManager subscriptionManager;
	private SubjectManager subjectManager;	
	
	private int termCode = 20081;
	
	public SubscriptionController() {
		subscriptionManager = EntityController.getInstance().getSubscriptionManager();
		subjectManager = EntityController.getInstance().getSubjectManager();
	}
	
	public List<Subscription> getSubscriptionsForStudent() {
		ArrayList<Subscription> list = new ArrayList<Subscription>();
		
		for (Subscription sb : subscriptionManager.getMap().values()) {
			if (sb.getStudent() == getStudentCode()) {
				list.add(sb);
			}
		}
		
		return list;
	}
	
	private List<Integer> getClassesForSubscriptions(List<Subscription> sbs) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for (Subscription s : sbs) {
			list.add(s.getClazz());
		}
		
		return list;
	}
	
	public void updateTables() {
		getPanel().clearTables();
		
		List<Subscription> subscriptions = getSubscriptionsForStudent();
		List<Integer> classes = getClassesForSubscriptions(subscriptions);
		
		for (Subscription s : subscriptions) {
			getPanel().addRowToCurrent(s.getCode(), s.getClazz(), s.getDate());
		}
		
		ClassManager classManager = EntityController.getInstance().getClassManager();
		
		for (Class clazz : classManager.getMap().values()) {
			if (clazz.getTerm() == termCode && !classes.contains(clazz.getCode())) {
				getPanel().addRowToAvailable(clazz.getCode(), 
						subjectManager.getObjectByKey(clazz.getSubject()).getName(),
						clazz.getSize(),
						getTeacherNameById(clazz.getTeacher()));
			}
		}
	}
	
	public void addSubscription(int classCode) {
		Integer code = subscriptionManager.nextIntID();
		
		Subscription sub = new Subscription();
		sub.setCode(code);
		sub.setClazz(classCode);
		sub.setStudent(getStudentCode());
		sub.setDate(Calendar.getInstance().getTime());
		
		try {
			subscriptionManager.add(sub);
			
			subscriptionManager.save();
			
			updateTables();
		} catch (EntityAlreadyExistsException e) {
			e.printStackTrace();
		}
	}
	
	public void removeSubscription(int subCode) {
		subscriptionManager.remove(subCode);
		
		updateTables();
	}

	public String getTeacherNameById(long code) { 
		TeacherManager teacherManager = EntityController.getInstance().getTeacherManager();
		
		return teacherManager.getObjectByKey(code).getName();
	}
	
	public void setTermCode(int termCode) {
		this.termCode = termCode;
	}
	
	private long getStudentCode() {
		return ControllerManager.getInstance().getLoginController().getStudentCode();
	}
	
	private SubscriptionPanel getPanel() {
		return (SubscriptionPanel) WindowManager.getInstance().getPanel("subscription.student");
	}
}
